package com.unionpay.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class StrUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static String getDate(Date date) {
		return sdf.format(date);
	}

	public static String Hex2Int(String str) {
		if (!StrUtil.hasEmpty(str) && str.startsWith("0x")) {
			str = String.valueOf(Integer.parseInt(str.substring(2), 16));
		}
		return str;
	}

	public static boolean hasEmpty(String... params) {
		for (String str : params) {
			if (str == null || str.isEmpty()) {
				return true;
			}
		}
		return false;
	}

	public static boolean isUrl(String url) {
		return url != null && !url.isEmpty() && url.startsWith("http://");
	}

	public static boolean is0x40(String str) {
		if (str.startsWith("0x")) {
			str = str.substring(2);
		}
		return str.length() == 40;
	}

	public static boolean is0x64(String str) {
		if (str.startsWith("0x")) {
			str = str.substring(2);
		}
		return str.length() == 64;
	}

	public static boolean is0x128(String str) {
		if (str.startsWith("0x")) {
			str = str.substring(2);
		}
		return str.length() == 128;
	}

	public static boolean isDateStr8(String date) {
		if (date != null && date.length() == 8) {
			try {
				int y = Integer.parseInt(date.substring(0, 4));
				int m = Integer.parseInt(date.substring(4, 6));
				int d = Integer.parseInt(date.substring(6, 8));
				return y > 1900 && y < 2200 && m > 0 && m < 13 && d > 0
						&& d < 32;
			} catch (Exception e) {
			}
		}
		return false;
	}

	public static boolean isDateStr14(String date) {
		if (date != null && date.length() == 14) {
			try {
				int y = Integer.parseInt(date.substring(0, 4));
				int m = Integer.parseInt(date.substring(4, 6));
				int d = Integer.parseInt(date.substring(6, 8));
				int hh = Integer.parseInt(date.substring(8, 10));
				int mm = Integer.parseInt(date.substring(10, 12));
				int ss = Integer.parseInt(date.substring(12, 14));
				return y > 1900 && y < 2200 && m > 0 && m < 13 && d > 0
						&& d < 32 && hh >= 0 && hh < 24 && mm >= 0 && mm < 60
						&& ss >= 0 && ss < 60;
			} catch (Exception e) {
			}
		}
		return false;
	}
}
